<?php

namespace app\admin\controller;

use think\Lang;

/**
 * ============================================================================
 * DSMall多用户商城
 * ============================================================================
 * 版权所有 2014-2028 长沙德尚网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.csdeshang.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * 控制器
 */
class Twogoods extends AdminControl
{

    public function _initialize()
    {
        parent::_initialize();
        Lang::load(APP_PATH . 'admin/lang/' . config('default_lang') . '/goodsclass.lang.php');
    }

    /**
     * AJAX获取商品分类
     */
    public function ajax_class()
    {
        $id = intval(input('param.id'));
        $deep = intval(input('param.deep'));
        if ($id <= 0 || $deep <= 0 || $deep >= 4) {
            echo 'false';
            exit();
        }
        $deep += 1;
        $goodsclass_model = model('goodsclass');

        // 验证分类是否存在
        $gc_info = $goodsclass_model->getGoodsclassInfoByIds($id);
        if (empty($gc_info)) {
            echo 'false';
            exit();
        }

        // 读取商品分类
        if ($deep != 4) {
            $gc_list = $goodsclass_model->getGoodsclass($this->_store_id, $id, $deep);
        }
        // 分类不为空输出分类信息
        if (!empty($gc_list)) {
            $data = array('type' => 'class', 'data' => $gc_list, 'deep' => $deep);
        } else {
            // 查询类型
            $type_model = model('type');
            $spec_list = $type_model->getSpecByType(array('type_id' => $gc_info['type_id']), 't.type_id, s.*');

            $data = array('type' => 'spec', 'data' => $spec_list, 'gcid' => $id, 'aa' => $gc_info, 'gcid' => $id, 'deep' => $deep);
        }

        echo json_encode($data);
        exit();
    }


    /**
     * Notes:审核通过的二手商品
     * User: shen
     * Date: 2020/5/21
     * Time: 15:21
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $file_http = db('config')->where('code', 'qiniu_http')->find();
        $this->assign('http_name', $file_http['value']);
        /**
         * 查询条件
         */
        $where = array();
        $where['is_two_goods'] = 1;
        $where['goods_lock'] = 0;
        $where['goods_status'] = array('in','1,3');
        #二手商品名称
        $search_goods_name = trim(input('param.search_goods_name'));
        if ($search_goods_name != '') {
            $where['goods_name'] = array('like', '%' . $search_goods_name . '%');
        }

        $b_id = intval(input('param.b_id'));
        if ($b_id > 0) {
            $where['brand_id'] = $b_id;
        }
        $phone = intval(input('param.phone'));
        if ($phone > 0) {
            $where['two_phone'] = $phone;
        }
        $status = input('param.status');
        if (!empty($status)) {
            $where['goods_status'] = $status;
        }

        $type = intval(input('param.types'));
        if ($type > 0) {
            if ($type == 1) {
                $where['store_id'] = 0;
            } else if ($type == 2) {
                $where['store_id'] = array('neq', '0');
            }
        }

        //时间
        $query_start_time = input('param.query_start_time');
        $query_end_time = input('param.query_end_time');
        $if_start_time = preg_match('/^20\d{2}-\d{2}-\d{2}$/', $query_start_time);
        $if_end_time = preg_match('/^20\d{2}-\d{2}-\d{2}$/', $query_end_time);
        $start_unixtime = $if_start_time ? strtotime($query_start_time) : null;
        $end_unixtime = $if_end_time ? strtotime($query_end_time) : null;
        if ($start_unixtime || $end_unixtime) {
            $where['goods_addtime'] = array('between', array($start_unixtime, $end_unixtime));
        }

        $goods_model = model('goods');
        $goods_list = $goods_model->getGoodsCommonList($where, '*', 10, 'goods_commonid desc,mall_goods_sort asc');

        foreach ($goods_list as $key => $value) {
            $image_info = db('goodsimages')->where(['goods_commonid' => $value['goods_commonid']])->find();
            if (!empty($image_info['goodsimage_url'])) {
                $goods_list[$key]['info_images'] = $image_info['goodsimage_url'];
            } else {
                $goods_list[$key]['info_images'] = 'http://33wang.douyinhot.com/eff1e202001141901358037.png';
            }

        }

        $this->assign('goods_list', $goods_list);
        $this->assign('show_page', $goods_model->page_info->render());

        $storage_array = $goods_model->calculateStorage($goods_list);
        $this->assign('storage_array', $storage_array);

        // 品牌
        $brand_list = model('brand')->getBrandPassedList(array());
        $this->assign('search', $where);
        $this->assign('brand_list', $brand_list);

        $this->assign('verify', array('1' => '上架', '0' => '待审核', '2' => '违规商品', '3' => '下架', '-2' => '已删除'));

        $this->assign('ownShopIds', array_fill_keys(model('store')->getOwnShopIds(), true));

        $type = input('param.type');
        if (!in_array($type, array('lockup', 'waitverify', 'allgoods'))) {
            $type = 'allgoods';
        }

        $this->assign('type', $type);
        $this->setAdminCurItem('two_goods');
        return $this->fetch();
    }


    /**
     * 违规下架二手商品
     */
    public function two_goods_no()
    {
        $file_http = db('config')->where('code', 'qiniu_http')->find();
        $this->assign('http_name', $file_http['value']);
        /**
         * 查询条件
         */
        $where = array();
        #二手商品名称
        $search_goods_name = trim(input('param.search_goods_name'));
        if ($search_goods_name != '') {
            $where['goods_name'] = array('like', '%' . $search_goods_name . '%');
        }
        $b_id = intval(input('param.b_id'));
        if ($b_id > 0) {
            $where['brand_id'] = $b_id;
        }
        $phone = intval(input('param.phone'));
        if ($phone > 0) {
            $where['two_phone'] = $phone;
        }
        $type = intval(input('param.types'));
        if ($type > 0) {
            if ($type == 1) {
                $where['store_id'] = 0;
            } else if ($type == 2) {
                $where['store_id'] = array('neq', '0');
            }
        }
        //时间
        $query_start_time = input('param.query_start_time');
        $query_end_time = input('param.query_end_time');
        $if_start_time = preg_match('/^20\d{2}-\d{2}-\d{2}$/', $query_start_time);
        $if_end_time = preg_match('/^20\d{2}-\d{2}-\d{2}$/', $query_end_time);
        $start_unixtime = $if_start_time ? strtotime($query_start_time) : null;
        $end_unixtime = $if_end_time ? strtotime($query_end_time) : null;
        if ($start_unixtime || $end_unixtime) {
            $where['goods_addtime'] = array('between', array($start_unixtime, $end_unixtime));
        }

        $goods_model = model('goods');
        $where['is_two_goods'] = 1;
        $where['goods_lock'] = 0;
        $where['goods_status'] = 2;
        $goods_list = $goods_model->getGoodsCommonList($where, '*', 10, 'goods_commonid desc,mall_goods_sort asc');
        foreach ($goods_list as $key => $value) {
            $image_info = db('goodsimages')->where(['goods_commonid' => $value['goods_commonid']])->find();
            if (!empty($image_info['goodsimage_url'])) {
                $goods_list[$key]['info_images'] = $image_info['goodsimage_url'];
            } else {
                $goods_list[$key]['info_images'] = 'http://33wang.douyinhot.com/eff1e202001141901358037.png';
            }

        }


        $this->assign('goods_list', $goods_list);
        $this->assign('show_page', $goods_model->page_info->render());

        $storage_array = $goods_model->calculateStorage($goods_list);
        $this->assign('storage_array', $storage_array);

        // 品牌
        $brand_list = model('brand')->getBrandPassedList(array());
        $this->assign('search', $where);
        $this->assign('brand_list', $brand_list);

        $this->assign('verify', array('1' => '上架', '0' => '待审核', '2' => '违规商品', '3' => '下架', '-2' => '已删除'));

        $this->assign('ownShopIds', array_fill_keys(model('store')->getOwnShopIds(), true));

        $type = input('param.type');
        if (!in_array($type, array('lockup', 'waitverify', 'allgoods'))) {
            $type = 'allgoods';
        }

        $this->assign('type', $type);
        $this->setAdminCurItem('two_goods_no');
        return $this->fetch('index');
    }

    /**
     * 待审核二手商品
     */
    public function two_goods_wait()
    {
        $file_http = db('config')->where('code', 'qiniu_http')->find();
        $this->assign('http_name', $file_http['value']);
        /**
         * 查询条件
         */
        $where = array();
        #二手商品名称
        $search_goods_name = trim(input('param.search_goods_name'));
        if ($search_goods_name != '') {
            $where['goods_name'] = array('like', '%' . $search_goods_name . '%');
        }
        $b_id = intval(input('param.b_id'));
        if ($b_id > 0) {
            $where['brand_id'] = $b_id;
        }
        $phone = intval(input('param.phone'));
        if ($phone > 0) {
            $where['two_phone'] = $phone;
        }
        $type = intval(input('param.types'));
        if ($type > 0) {
            if ($type == 1) {
                $where['store_id'] = 0;
            } else if ($type == 2) {
                $where['store_id'] = array('neq', '0');
            }
        }

        //时间
        $query_start_time = input('param.query_start_time');
        $query_end_time = input('param.query_end_time');
        $if_start_time = preg_match('/^20\d{2}-\d{2}-\d{2}$/', $query_start_time);
        $if_end_time = preg_match('/^20\d{2}-\d{2}-\d{2}$/', $query_end_time);
        $start_unixtime = $if_start_time ? strtotime($query_start_time) : null;
        $end_unixtime = $if_end_time ? strtotime($query_end_time) : null;
        if ($start_unixtime || $end_unixtime) {
            $where['goods_addtime'] = array('between', array($start_unixtime, $end_unixtime));
        }

        $goods_model = model('goods');
        $where['is_two_goods'] = 1;
        $where['goods_lock'] = 0;
        $where['goods_status'] = 0;
        $goods_list = $goods_model->getGoodsCommonList($where, '*', 10, 'goods_commonid desc,mall_goods_sort asc');
        foreach ($goods_list as $key => $value) {
            $image_info = db('goodsimages')->where(['goods_commonid' => $value['goods_commonid']])->find();
            if (!empty($image_info['goodsimage_url'])) {
                $goods_list[$key]['info_images'] = $image_info['goodsimage_url'];
            } else {
                $goods_list[$key]['info_images'] = 'http://33wang.douyinhot.com/eff1e202001141901358037.png';
            }

        }


        $this->assign('goods_list', $goods_list);
        $this->assign('show_page', $goods_model->page_info->render());

        $storage_array = $goods_model->calculateStorage($goods_list);
        $this->assign('storage_array', $storage_array);

        // 品牌
        $brand_list = model('brand')->getBrandPassedList(array());
        $this->assign('search', $where);
        $this->assign('brand_list', $brand_list);

        $this->assign('verify', array('1' => '上架', '0' => '待审核', '2' => '违规商品', '3' => '下架', '-2' => '已删除'));

        $this->assign('ownShopIds', array_fill_keys(model('store')->getOwnShopIds(), true));

        $type = input('param.type');
        if (!in_array($type, array('lockup', 'waitverify', 'allgoods'))) {
            $type = 'allgoods';
        }

        $this->assign('type', $type);
        $this->setAdminCurItem('two_goods_wait');
        return $this->fetch('index');
    }


    /**
     * 添加二手商品
     */
    public function two_goods_add()
    {
        $file_http = db('config')->where('code', 'qiniu_http')->find();
        $this->assign('http_name', $file_http['value']);
        $goodsclass_model = model('goodsclass');
        $brand_list = model('brand')->getBrandPassedList(array());
        $this->assign('brand_list', $brand_list);
        $area_model = model('area');
        if (!request()->isPost()) {
            $type = [
                'class_id' => 0,
            ];
            $this->assign('type', $type);
            #获取顶级分类
            $where = array();
            $where['gc_parent_id'] = 0;
            $where['gc_show'] = 1;
            $field = '';
            $field = 'gc_id,gc_name';
            $top_goods_class = $goodsclass_model->getgoodsclassinfo($where, $field);
            if (empty($top_goods_class)) {
                $this->error('暂无顶级分类');
            }

            #获取品牌
            $field = '';
            //父类列表，只取到第二级
            $parent_list = $goodsclass_model->getTreeClassList(2);
            $gc_list = array();
            if (is_array($parent_list)) {
                foreach ($parent_list as $k => $v) {
                    $parent_list[$k]['gc_name'] = str_repeat("&nbsp;", $v['deep'] * 2) . $v['gc_name'];
                    if ($v['deep'] == 1)
                        $gc_list[$k] = $v;
                }
            }
            $this->assign('gc_list', $gc_list);
            $this->assign('top_goods_class', $top_goods_class);
            $this->setAdminCurItem('two_goods_add');
            return $this->fetch('two_goods_add');
        } else {
            $insert_array = array();
            $goods_name = trim(input('post.goods_name'));
            $insert_common_array = array();
            $insert_common_array['goods_name'] = $goods_name;
            if (trim(input('post.class_id'))) {
                $class_id = intval(input('post.class_id'));
                $class_name = input('class_name');
                $class_info = db('goodsclass')->where(['gc_id' => $class_id])->find();
                if ($class_info['gc_parent_id'] == 0) {
                    #顶级
                    $insert_common_array['gc_id_1'] = $class_id;
                    $insert_common_array['gc_name'] = $class_name;
                } else {
                    #查询判断父级信息
                    $pranet_info = db('goodsclass')->where('gc_id', $class_info['gc_parent_id'])->find();
                    if ($pranet_info['gc_parent_id'] == 0) {
                        #第二级
                        $insert_common_array['gc_id_1'] = $class_info['gc_parent_id'];
                        $insert_common_array['gc_id_2'] = $class_id;
                        $insert_common_array['gc_name'] = $pranet_info['gc_name'] . ',' . $class_name;
                    } else {
                        $pranets_info = db('goodsclass')->where('gc_id', $pranet_info['gc_parent_id'])->find();
                        $insert_common_array['gc_id'] = $class_id;
                        $insert_common_array['gc_id_1'] = $pranets_info['gc_id'];
                        $insert_common_array['gc_id_2'] = $pranet_info['gc_id'];
                        $insert_common_array['gc_name'] = $pranets_info['gc_name'] . ',' . $pranet_info['gc_name'] . ',' . $class_name;
                    }

                }


            }
            if (trim(input('post.b_id')) == 0) {

            } else {
                $brand_id = intval(input('post.b_id'));
                $brand_name = input('post.b_name');
            }

            $count = intval(input('post.count'));


            $phone = input('post.phone');

            $price = input('post.price');


            $area_list = input('post.company_address');


            $content = input('post.content');

            $files = input('pic/a');

            $insert_common_array['store_id'] = 0;
            $insert_common_array['store_name'] = '平台';
            $insert_common_array['brand_id'] = $brand_id ? $brand_id : '';
            $insert_common_array['brand_name'] = $brand_name ? $brand_name : '';
            $insert_common_array['goods_image'] = $files['-11'];
            $insert_common_array['goods_state'] = 1;
            $insert_common_array['goods_verify'] = 1;
            $insert_common_array['goods_addtime'] = time();
            $insert_common_array['goods_price'] = $price;
            $insert_common_array['two_goods_address'] = $area_list;
            $insert_common_array['goods_body'] = $content;
            $insert_common_array['goods_status'] = input('param.goods_status');
            $insert_common_array['is_two_goods'] = 1;
            $insert_common_array['two_phone'] = $phone;
            $insert_common_array['goods_status'] = input('goods_status');
            $insert_common_array['two_is_price'] = input('two_is_price');
            $new_id = db('goodscommon')->insertGetId($insert_common_array);
            if ($new_id) {
                $new_info = db('goodscommon')->where('goods_commonid', $new_id)->find();
                $insert_goods_array = array();
                $insert_goods_array['goods_commonid'] = $new_id;
                $insert_goods_array['goods_name'] = $goods_name;
                $insert_goods_array['gc_id'] = $new_info['gc_id'];
                $insert_goods_array['gc_id_1'] = $new_info['gc_id_1'];
                $insert_goods_array['gc_id_2'] = $new_info['gc_id_2'];
                $insert_goods_array['store_id'] = 0;
                $insert_goods_array['store_name'] = '平台';
                $insert_goods_array['brand_id'] = $new_info['brand_id'];
                $insert_goods_array['goods_image'] = $files['-11'];
                $insert_goods_array['goods_state'] = 1;
                $insert_goods_array['goods_verify'] = 1;
                $insert_goods_array['goods_addtime'] = time();
                $insert_goods_array['goods_price'] = $price;
                $insert_goods_array['goods_promotion_price'] = $price;
                $insert_goods_array['is_two_goods'] = 1;
                $insert_goods_array['goods_storage'] = $count;
                $newgoods_id = db('goods')->insertGetId($insert_goods_array);
                $file_http = db('config')->where('code', 'qiniu_http')->find();


                foreach ($files as $k => $v) {
                    $data = array();

                    $data = ['goods_commonid' => $new_id, 'store_id' => 0, 'color_id' => 0, 'goodsimage_url' => $file_http['value'] . '/' . $v];
                    if ($k == '-11') {
                        $data['goodsimage_isdefault'] = 1;
                    } else {
                        $data['goodsimage_isdefault'] = 0;
                    }

                    db('goodsimages')->data($data)->insert();
                }

                if ($newgoods_id) {
                    return json(['url' => url('Twogoods/index'), 'code' => 1, 'msg' => '添加成功']);
                } else {
                    return json(['url' => url('Twogoods/index'), 'code' => 0, 'msg' => '添加失败']);
                }

            }


        }
    }

    /**
     * 编辑二手商品
     */
    public function edit_goods()
    {
        $file_http = db('config')->where('code', 'qiniu_http')->find();
        $this->assign('http_name', $file_http['value']);

        $id = input('param.commonid');
        if (empty($id)) {
            $this->error('请选择要操作的商品');
        }
        $goods_info = db('goodscommon')->where('goods_commonid', $id)->find();
        if (empty($goods_info) || $goods_info['goods_lock'] == 1) {
            $this->error('操作错误');
        }
        /* if ($goods_info['goods_status'] == 1) {
             $this->error('请将商品下架后处理');
         }*/

        $goodsclass_model = model('goodsclass');
        $area_model = model('area');
        $brand_model = model('brand');
        if (!request()->isPost()) {
            $type = [
                'class_id' => 0,
            ];
            $this->assign('type', $type);
            $goods_list = db('goods')->where('goods_commonid', $id)->find();
            $goods_info['goods_storage'] = $goods_list['goods_storage'];
            $this->assign('goods_info', $goods_info);
            $goods = db('goods')->where('goods_commonid', $id)->find();
            $goods_images = db('goodsimages')->where('goods_commonid', $id)->select();
            $this->assign('goods_images', $goods_images);
            $area_info = explode(',', $goods_info['two_goods_address']);
            $this->assign('area_info', $area_info);
            #获取手机顶级分类
            $where = array();
            $where['gc_parent_id'] = 0;
            $where['gc_show'] = 1;
            $field = '';
            $field = 'gc_id,gc_name';
            $top_goods_class = $goodsclass_model->getgoodsclassinfo($where, $field);
            if (empty($top_goods_class)) {
                $this->error('暂无顶级分类');
            }

            #获取省份
            $where = array();
            $where['area_parent_id'] = 0;
            $top_area_class = $area_model->getarea_list($where, 'area_id,area_name');
            if (empty($top_area_class)) {
                $this->error('暂无省份');
            }
            $this->assign('top_area_class', $top_area_class);
            #获取品牌
            $field = '';
            //父类列表，只取到第二级
            $parent_list = $goodsclass_model->getTreeClassList(2);
            $gc_list = array();
            if (is_array($parent_list)) {
                foreach ($parent_list as $k => $v) {
                    $parent_list[$k]['gc_name'] = str_repeat("&nbsp;", $v['deep'] * 2) . $v['gc_name'];
                    if ($v['deep'] == 1)
                        $gc_list[$k] = $v;
                }
            }
            $this->assign('gc_list', $gc_list);

            $brand_list = $brand_model->getbrand_list($field);

            if (empty($brand_list)) {
                $this->error('暂无品牌');
            }


            $this->assign('brand_list', $brand_list);


            $this->assign('top_goods_class', $top_goods_class);
            $this->setAdminCurItem('edit_goods');
            return $this->fetch('edit_goods');
        } else {
            $insert_array = array();
            $goods_name = trim(input('post.goods_name'));
            $insert_common_array = array();
            $insert_common_array['goods_name'] = $goods_name;

            if (trim(input('post.class_id')) && input('post.class_id') != '1000000') {
                $class_id = intval(input('post.class_id'));
                $class_name = input('post.class_name');
                $class_info = db('goodsclass')->where(['gc_id' => $class_id])->find();
                if ($class_info['gc_parent_id'] == 0) {
                    #顶级
                    $insert_common_array['gc_id_1'] = $class_id;
                    $insert_common_array['gc_name'] = $class_name;
                } else {
                    #查询判断父级信息
                    $pranet_info = db('goodsclass')->where('gc_id', $class_info['gc_parent_id'])->find();
                    if ($pranet_info['gc_parent_id'] == 0) {
                        #第二级
                        $insert_common_array['gc_id_1'] = $class_info['gc_parent_id'];
                        $insert_common_array['gc_id_2'] = $class_id;
                        $insert_common_array['gc_name'] = $pranet_info['gc_name'] . ',' . $class_name;
                    } else {
                        $pranets_info = db('goodsclass')->where('gc_id', $pranet_info['gc_parent_id'])->find();
                        $insert_common_array['gc_id'] = $class_id;
                        $insert_common_array['gc_id_1'] = $pranets_info['gc_id'];
                        $insert_common_array['gc_id_2'] = $pranet_info['gc_id'];
                        $insert_common_array['gc_name'] = $pranets_info['gc_name'] . ',' . $pranet_info['gc_name'] . ',' . $class_name;

                    }

                }
            } else if (input('post.class_id') == '1000000') {

            } else {
                $insert_common_array['gc_id'] = '';
                $insert_common_array['gc_id_1'] = '';
                $insert_common_array['gc_id_2'] = '';
                $insert_common_array['gc_name'] = '';
            }

            if (trim(input('post.b_id')) == 0) {
                $brand_id = '0';

                $brand_name = '';
            } else {
                $brand_id = intval(input('post.b_id'));
                $brand_name = input('post.b_name');
            }

            $count = intval(input('post.count'));


            $phone = input('post.phone');


            $price = intval(input('post.price'));

            if (input('post.district_id') != 1) {
                $area_list = input('post.company_address');
                $insert_common_array['two_goods_address'] = $area_list;
            }
            $content = input('post.content');

            $pic = input('param.pic/a');
            if ($pic['0'] != $goods_info['goods_image']) {
                $insert_common_array['goods_image'] = $pic['0'];
            }

            $insert_common_array['brand_id'] = $brand_id ? $brand_id : '';
            $insert_common_array['brand_name'] = $brand_name ? $brand_name : '';
            $insert_common_array['goods_price'] = $price;
            $insert_common_array['goods_body'] = $content;
            $insert_common_array['two_phone'] = $phone;
            $insert_common_array['two_is_price'] = input('param.two_is_price');;
            $res = db('goodscommon')->where('goods_commonid', $id)->data($insert_common_array)->update();

            $new_info = db('goodscommon')->where('goods_commonid', $id)->find();
            $insert_goods_array = array();
            $insert_goods_array['goods_name'] = $goods_name;
            $insert_goods_array['gc_id'] = $new_info['gc_id'];
            $insert_goods_array['gc_id_1'] = $new_info['gc_id_1'];
            $insert_goods_array['gc_id_2'] = $new_info['gc_id_2'];
            $insert_goods_array['brand_id'] = $new_info['brand_id'];
            $insert_goods_array['goods_image'] = $new_info['goods_image'];
            $insert_goods_array['goods_price'] = $price;
            $insert_goods_array['goods_promotion_price'] = $price;
            $insert_goods_array['goods_storage'] = $count;
            $newgoods_id = db('goods')->where('goods_commonid', $id)->data($insert_goods_array)->update();
            //p($newgoods_id);die;
            db('goodsimages')->where('goods_commonid', $id)->delete();
            foreach ($pic as $k => $v) {
                $data = array();

                $data = ['goods_commonid' => $id, 'store_id' => 0, 'color_id' => 0, 'goodsimage_url' => $v];
                if ($k == 0) {
                    $data['goodsimage_isdefault'] = 1;
                } else {
                    $data['goodsimage_isdefault'] = 0;
                }

                db('goodsimages')->data($data)->insert();
            }
            return json(['url' => url('Twogoods/index'), 'code' => 1, 'msg' => '修改成功']);
        }
    }

    #上下架
    public function goods_status()
    {
        $id = input('param.commonid');

        $goods_info = db('goodscommon')->where('goods_commonid', $id)->where('goods_lock', 0)->find();
        if (empty($goods_info)) {
            ds_json_encode(10001, '操作失败');
        }
        if ($goods_info['goods_status'] == 1) {
            #下架操作
            $goods_status = 3;
        } else {
            #上架操作
            $goods_status = 1;
        }

        $return = db('goodscommon')->where('goods_commonid', $id)->data(['goods_status' => $goods_status])->update();
        if ($return) {
            // 添加操作日志
            $this->log('商品上下架commonid' . $id, 1);
            ds_json_encode(10000, '操作成功');
        } else {
            $this->log('商品上下架commonid' . $id, 0);
            ds_json_encode(10001, '操作失败');
        }

    }

    /**
     * Notes:审核二手商品
     * User: shen
     * Date: 2020/5/18
     * Time: 14:38
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function goods_verify()
    {
        $id = input('param.commonid');

        if (input('verify_state') == 1) {
            #审核通过
            $data['goods_status'] = 1;

        } else {
            #审核失败
            $data['goods_status'] = 2;
            $data['verify_reason'] = input('verify_reason');
        }

        $return = db('goodscommon')->where('goods_commonid', $id)->update($data);
        if ($return) {
            // 添加操作日志
            $this->log('商品审核commonid' . $id, 1);
            dsLayerOpenSuccess(lang('ds_common_op_succ'));

        } else {
            $this->log('商品审核commonid' . $id, 0);
            dsLayerOpenSuccess('审核失败');

        }
    }

    /**
     * Notes:二手商品详情
     * User: shen
     * Date: 2020/5/18
     * Time: 14:38
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function goods_content()
    {
        $file_http = db('config')->where('code', 'qiniu_http')->find();
        $this->assign('http_name', $file_http['value']);
        $id = input('param.commonid');
        $goods_info = db('goodscommon')->where('goods_commonid', $id)->where('goods_lock', 0)->find();
        //p($goods_info);die;
        if (empty($goods_info)) {
            $this->error('操作失败');
        }
        $goods = db('goods')->where('goods_commonid', $id)->find();
        $goods_info['count'] = $goods['goods_storage'];
        $this->assign('goods_info', $goods_info);
        $this->assign('is_price', array('0' => '不可议价', '1' => '可议价'));
        $this->assign('is_status', array('0' => '使用中', '1' => '已拆除'));

        $goods_images = db('goodsimages')->where('goods_commonid', $id)->select();
        if (!empty($goods_images)) {
            $this->assign('goods_img', $goods_images);
        }

        return $this->fetch();
    }


    /**
     * AJAx 获取分类
     */
    public function ajax_get_goods_class()
    {
        if (request()->isAjax()) {
            $goodsclass_model = model('goodsclass');
            $gc_id = trim(input('gc_id'));
            #获取手机顶级分类
            $where = array();
            $where['gc_parent_id'] = $gc_id;
            $where['gc_show'] = 1;
            $field = '';
            $field = 'gc_id,gc_name';
            $goods_class = $goodsclass_model->getgoodsclassinfo($where, $field);
            return $goods_class;
        } else {
            $this->error('访问错误');
        }


    }

    /**
     * AJAx 获取地区
     */
    public function ajax_get_area_class()
    {
        if (request()->isAjax()) {
            $area_model = model('area');
            $area_id = trim(input('area_id'));
            #获取手机顶级分类
            $where = array();
            $where['area_parent_id'] = $area_id;
            $field = '';
            $field = 'area_id,area_name';
            $area_class = $area_model->getarea_list($where, $field);
            return $area_class;
        } else {
            $this->error('访问错误');
        }

    }

    public function drop_goods()
    {

        $commonid = input('param.commonid');

        $return = db('goodscommon')->where('goods_commonid', $commonid)->data(['goods_lock' => 1])->update();
        if ($return) {
            // 添加操作日志
            $this->log('删除商品commonid' . $commonid, 1);
            ds_json_encode(10000, '删除成功');
        } else {
            $this->log('删除商品commonid' . $commonid, 0);
            ds_json_encode(10001, '删除失败');
        }
    }

    /*
      ajax 上传图片

     */
    public function ajax_upload()
    {
        $file_type = array('0' => 'image/jpeg', '1' => 'image/jpg', '2' => 'image/png');
        if (in_array($_FILES['file']['type'], $file_type)) {
            $file_name = input('post.name');
            $upload_info = upload_qiniu('file');
            return $upload_info;
        } else {
            return $data['msg'] = 0;

        }
    }

    /**
     * 获取卖家栏目列表,针对控制器下的栏目
     */
    protected function getAdminItemList()
    {
        $menu_array = array(
            array(
                'name' => 'two_goods',
                'text' => '二手商品',
                'url' => url('Twogoods/index')
            ),
            array(
                'name' => 'two_goods_no',
                'text' => '违规二手商品',
                'url' => url('Twogoods/two_goods_no')
            ),
            array(
                'name' => 'two_goods_wait',
                'text' => lang('two_goods_wait'),
                'url' => url('Twogoods/two_goods_wait')
            ),
            array(
                'name' => 'two_goods_add',
                'text' => '添加二手商品',
                'url' => url('Twogoods/two_goods_add')
            ),

        );
        if (request()->action() === 'edit_goods') {
            $menu_array[] = array(
                'name' => 'edit_goods',
                'text' => '修改二手商品',
                'url' => url('Twogoods/edit_goods')
            );
        }


        return $menu_array;
    }

}
